package com.ruoyi.web.controller.assets;

import com.ruoyi.assets.domain.StateCollection;
import com.ruoyi.assets.service.IStateCollectionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/assets/collection")
public class StateCollectionController extends BaseController {
    @Autowired
    private IStateCollectionService stateCollectionService;

    /**
     * 查询收藏列表
     */
    @PreAuthorize("@ss.hasPermi('assets:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(StateCollection stateCollection) {
        startPage();
        List<StateCollection> list = stateCollectionService.selectStateCollectionList(stateCollection);
        return getDataTable(list);
    }

    /**
     * 获取当前用户的收藏列表
     */
    @GetMapping("/my")
    public AjaxResult myCollections() {
        Long userId = SecurityUtils.getUserId();
        List<StateCollection> list = stateCollectionService.selectCollectionsByUserId(userId);
        return success(list);
    }

    /**
     * 添加收藏
     */
    @PreAuthorize("@ss.hasPermi('assets:collection:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping("/{assetId}")
    public AjaxResult add(@PathVariable("assetId") Long assetId) {
        Long userId = SecurityUtils.getUserId();
        return toAjax(stateCollectionService.addCollection(userId, assetId));
    }

    /**
     * 取消收藏
     */
    @PreAuthorize("@ss.hasPermi('assets:collection:remove')")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{assetId}")
    public AjaxResult cancel(@PathVariable("assetId") Long assetId) {
        Long userId = SecurityUtils.getUserId();
        return toAjax(stateCollectionService.cancelCollection(userId, assetId));
    }
    
    /**
     * 检查是否已收藏
     */
    @GetMapping("/check/{assetId}")
    public AjaxResult check(@PathVariable("assetId") Long assetId) {
        Long userId = SecurityUtils.getUserId();
        boolean isCollected = stateCollectionService.isCollected(userId, assetId);
        return success(isCollected);
    }
} 